﻿using BusinessLogic;
using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Logic;
using Common;
using System.IO;

namespace AppAdmin.Controllers
{
    public class ActivityController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult Edit()
        {
            return View();
        }
        
        [HttpGet]
        public ActionResult GetGridJson(Pagination pagination,string Title,string StartTime,string EndTime)
        {
            var expression = ExtLinq.True<tb_News>();
            expression = expression.And(t => t.IsDeleted == 0);
            if (!string.IsNullOrEmpty(Title))
            {
                expression = expression.And(t => t.Title.Contains(Title.Trim()));
            }
            if (!string.IsNullOrEmpty(StartTime))
            {
                var start = Convert.ToDateTime(StartTime);
                expression = expression.And(t => t.AddTime >= start);
            }
            if (!string.IsNullOrEmpty(EndTime))
            {
                var end = Convert.ToDateTime(EndTime).AddDays(1);
                expression = expression.And(t => t.AddTime < end);
            }
            int Total = 0;
            var list = new NewsLogic().getPageDate<tb_News, DateTime?>(c => new { c.ID, c.Title, c.Pic, c.Description, c.AddPerson, c.AddTime}, expression, o => o.AddTime, pagination.page, pagination.rows, out Total);
            pagination.records = Total;
            var data = new
            {
                rows = list,
                total = pagination.total,
                page = pagination.page,
                records = pagination.records
            };
            return Content(data.ToJson());
        }
        public ActionResult Details()
        {
            return View();
        }
        public ActionResult GetFormJson(string keyValue)
        {
            var data = new NewsLogic().GetForm(keyValue);
            return Content(data.ToJson());
        }

        public ActionResult DeleteForm(string keyValue)
        {
            List<string> strList = new List<string>();
            UserCommentLogic bll = new UserCommentLogic();
            if (keyValue.IndexOf(',') < 0)
            {
                strList.Add(keyValue);
            }
            else
            {
                string[] strs = keyValue.Split(',');
                for (int i = 0; i < strs.Length; i++)
                {
                    if (!string.IsNullOrEmpty(strs[i]) && CommonFun.IsGuidByReg(strs[i]))
                    {
                        strList.Add(strs[i]);
                    }
                }
            }
            bll.DelInfo(strList);
            return Content(new AjaxResult { state = ResultType.success.ToString(), message = "操作成功。" }.ToJson());
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult SubmitForm(FormCollection collection, string keyValue)
        {
            try {
                tb_News model = new tb_News();
                NewsLogic bll = new NewsLogic();
                model.Title = collection["Title"].Trim();
                model.Description = collection["Description"].Trim();
                model.Details = collection["Details"].Trim();

                model.Pic = collection["Pic"].Trim();
                if (!string.IsNullOrEmpty(keyValue))//修改
                {
                    model.ID = keyValue;
                    bll.UpdateInfo(model);
                }
                else
                {
                    model.AddTime = DateTime.Now;
                    model.AddPerson = ThisAdminSession.LoginName;
                    model.ID = Guid.NewGuid().ToString();
                    model.IsDeleted = 0;
                    bll.AddInfo(model);
                }
            }
            catch (Exception ex)
            {

            }

            return Content(new AjaxResult { state = ResultType.success.ToString(), message = "操作成功。" }.ToJson());
        }
    }
}
